export default {
    name: "app",

    data() {
        return {
            activeIndex: '',

            userName: "未登录",

            // login_register按钮样式：
            login_register: {
                display: ''
            },
        };
    },
    created() {
        this.saveFirstNavState();
        this.activePath = window.sessionStorage.getItem("activePath");
        if (window.sessionStorage.getItem("token") != null) {
            this.login_register.display = 'none';
            this.getUser();
        }
        this.setBadge();
    },
    methods: {
        // 退出登录
        logout() {
            window.sessionStorage.clear();
            this.$router.push("/login_register");
        },
        // 页面跳转
        to(web) {
            switch (web) {
                case 1:
                    this.$router.push('/login_register');
                    break;
                case 2:
                    this.saveNavState('cart')
                    this.$router.push('/cart');
                    break;
                case 3:
                    window.sessionStorage.setItem("activePath2", "personal");
                    this.$router.push('/personal');
                    break;
            }
        },

        // 保存链接激活状态
        saveFirstNavState() {
            if (window.sessionStorage.getItem("activePath") == null) {
                window.sessionStorage.setItem("activePath", "home");
            }
        },
        saveNavState(activePath) {
            window.sessionStorage.setItem("activePath", activePath);
            this.activePath = activePath;
        },

        // 获取用户信息
        async getUser() {
            this.$http.interceptors.request.use((config) => {
                config.headers.authentication = window.sessionStorage.getItem("token");
                return config;
            });

            const { data: res } = await this.$http.post('/cs/api/user/current');
            // console.log(res)
            // userId保存到客户端的sessionStorage中
            window.sessionStorage.setItem('userId', res.response.id);

            this.userName = res.response.userName;
            this.shoppingCartNum = res.response.shoppingCartNum;
            this.notYetShipNum = res.response.notYetShipNum;
            this.awaitNum = res.response.awaitNum;
        },
    },
};